package isquirrel.servlets.handler;

import isquirrel.UserManager;
import isquirrel.beans.User;
import isquirrel.error.CannotLoginUserException;
import isquirrel.util.HibernateUtil;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.hibernate.Session;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public abstract class HttpDBServlet extends HttpServlet {

	private static final long serialVersionUID = 3775923761440550801L;

	protected HttpServletRequest request;
	protected HttpServletResponse response;
		
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.request = request;
		this.response = response;
		 response.setContentType("text/html; charset=utf-8");
		Session session = HibernateUtil.getSession();
		try {
			session.beginTransaction();
			handleGet();
			session.getTransaction().commit();
		} catch (Exception e) {
			e.printStackTrace();
			//throw a servlet exception
			if (ServletException.class.isInstance(e)) {
				//throw (ServletException) e;
				//handleError();
			} else {
				//throw new ServletException(e);
				//handleError();
			}
		}
	}
	

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.request = request;
		this.response = response;
		response.setContentType("text/html; charset=utf-8");
		Session session = HibernateUtil.getSession();
		try {
			session.beginTransaction();
			handlePost();
			session.getTransaction().commit();
		} catch (Exception e) {
			//throw a servlet exception
			if (ServletException.class.isInstance(e)) {
//				throw (ServletException) e; 
				//handleError();
			} else {
	//			throw new ServletException(e);
			//	handleError();
			}
		}
	}
	
	protected void writeAjaxResponse(Object json) throws JSONException {
		PrintWriter out = null;
		try {
			out = response.getWriter();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		if (json instanceof JSONArray){
			out.print(((JSONArray) json).toString());
		}
		else if (json instanceof JSONObject){
			out.print(((JSONObject) json).toString());
		}
		else {
			out.print((String)json);
		}
	}
	
	public static boolean isUserLoggedIn(HttpServletRequest request) {
		boolean isUserLoggedIn = false;
		HttpSession  session = request.getSession();
		String user = (String) session.getAttribute("user");
		if (user != null) {
			isUserLoggedIn = true; 
		}
		return isUserLoggedIn;
	}
	
	protected boolean isUserLoggedIn() {
		return isUserLoggedIn(request);
	}
	
	protected User getLoggedInUser() {
		User user = null;
		if (isUserLoggedIn()) {
			HttpSession session = request.getSession();
			String username = (String)session.getAttribute("user");
			user = UserManager.getUser(username);
		}
		return user;
	}
	
	protected void redirectToURL(String jspPage) {
		try {
			response.sendRedirect(jspPage);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	protected abstract void handlePost() throws ServletException, IOException, JSONException, CannotLoginUserException;
	
	protected abstract void handleGet() throws ServletException, IOException, JSONException,CannotLoginUserException;
	
	public void sendParameter(String name,Object value){
		
		request.setAttribute(name, value);
		
	}
	

}
